﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ScrumProject.Models;

namespace ScrumProject.Controllers
{ 
    public class SprintController : Controller
    {
        private SMRPO6Context db = new SMRPO6Context();
        SprintRepository sprintR = new SprintRepository();
        //
        // GET: /Sprint/

        public ViewResult Index(int projectId)
        {
            ViewBag.projectId = projectId;
            var sprints = db.Sprints.Include(s => s.Project).Include(s => s.Story);
            return View(sprintR.GetAllSprintsByProject(projectId).ToList());
        }

        //
        // GET: /Sprint/Details/5

        public ViewResult Details(int id)
        {
            Sprint sprint = db.Sprints.Find(id);
            ViewBag.projectId = sprint.ProjectId;
            return View(sprint);
        }

        //
        // GET: /Sprint/Create

        public ActionResult Create(int projectId)
        {
            Sprint sprint = new Sprint();
            sprint.ProjectId = projectId;
            ViewBag.newId = sprint.ProjectId;
            return View();
        }

        public bool IsSprintDateValid(Sprint sprint) 
        {
            List<DateTime> sd = sprintR.GetAllSprintStartDatesByProject1(sprint.ProjectId.Value, sprint.Id).ToList<DateTime>();
            List<DateTime> ed = sprintR.GetAllSprintEndDatesByProject1(sprint.ProjectId.Value, sprint.Id).ToList<DateTime>();

            int startSize = sd.Count;            

            bool add = false;

            if (startSize > 0)
            {
                for (int i = 0; i < startSize; i++)
                {
                    if (((DateTime.Compare(sd[i], sprint.DateFrom) < 0) && (DateTime.Compare(ed[i], sprint.DateFrom) < 0))
                        || ((DateTime.Compare(sd[i], sprint.DateFrom) > 0) && (DateTime.Compare(sd[i], sprint.DateTo) > 0)))
                    {
                        add = true;
                    }
                    else if (((DateTime.Compare(sd[i], sprint.DateFrom) == 0) && (DateTime.Compare(ed[i], sprint.DateTo) == 0)) 
                        && ((sprintR.GetSprintByStartDate(sd[i]) == sprintR.GetSprintByStartDate(sprint.DateFrom)) 
                            && (sprintR.GetSprintByEndDate(ed[i]) == sprintR.GetSprintByEndDate(sprint.DateTo))) )
                    {
                        add = true;
                    }
                    else
                    {
                        add = false;
                        break;
                    }
                }

            }
            else add = true;
            return add;
        }

        //
        // POST: /Sprint/Create

        [HttpPost]
        public ActionResult Create(Sprint sprint)
        {
            if (ModelState.IsValid)
            {
                //start date as today
                if (DateTime.Compare(sprint.DateFrom, DateTime.Now.Date) == 0)
                {
                    if (IsSprintDateValid(sprint) == true)
                    {
                        sprintR.AddSprint(sprint);
                        //db.Sprints.Add(sprint);
                        //db.SaveChanges();
                        return RedirectToAction("Index", new { projectId = sprint.ProjectId });
                    }

                    else
                    {
                        ModelState.AddModelError("", "Your sprint period is overlaping with an existing sprint");
                    }
                   
                }
                
                if (DateTime.Compare(sprint.DateFrom, DateTime.Now) < 0)
                {
                    ModelState.AddModelError("", "Start date of the sprint is in the past!");
                }

                //new start date
                else if(DateTime.Compare(sprint.DateFrom, sprint.DateTo) < 0)
                {
                    if (IsSprintDateValid(sprint) == true)
                    {
                        sprintR.AddSprint(sprint);
                        //db.Sprints.Add(sprint);
                        //db.SaveChanges();
                        return RedirectToAction("Index", new { projectId = sprint.ProjectId });
                    }

                    else
                    {
                        ModelState.AddModelError("", "Your sprint period is overlaping with an existing sprint");
                    }

                }                
                else if (DateTime.Compare(sprint.DateFrom, sprint.DateTo) == 0)
                {
                    ModelState.AddModelError("", "Start and end date of the sprint are in the same day, they have to be at least a day apart!");
                }
                else if (DateTime.Compare(sprint.DateFrom, sprint.DateTo) >= 0) 
                {
                    ModelState.AddModelError("", "Start date of the sprint is after its end date!");
                }
                
            }

            ViewBag.newId = sprint.ProjectId;
            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", sprint.ProjectId);
            ViewBag.StoryId = new SelectList(db.Stories, "Id", "Name", sprint.StoryId);
            return View(sprint);
        }
        
        //
        // GET: /Sprint/Edit/5
 
        public ActionResult Edit(int id)
        {
            Sprint sprint = db.Sprints.Find(id);
            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", sprint.ProjectId);
            ViewBag.StoryId = new SelectList(db.Stories, "Id", "Name", sprint.StoryId);
            ViewBag.newId = sprint.ProjectId;
            return View(sprint);
        }

        //
        // POST: /Sprint/Edit/5

        [HttpPost]
        public ActionResult Edit(Sprint sprint)
        {
            if (ModelState.IsValid)
            {
                if (DateTime.Compare(sprint.DateFrom, DateTime.Now.Date) == 0)
                {
                    Sprint s = sprintR.GetSprint(sprint.Id);
                    if (sprint.ProjectId == null)
                        sprint.ProjectId = s.ProjectId;

                    if (IsSprintDateValid(sprint) == true)
                    {
                       
                        sprintR.UpdateSprint(sprint);
                        //db.Entry(sprint).State = EntityState.Modified;
                        //db.SaveChanges();
                        return RedirectToAction("Index", new { projectId = sprint.ProjectId });
                    }
                    else 
                    {
                        ModelState.AddModelError("", "Your sprint period is overlaping with an existing sprint");
                    }

                    ViewBag.newId = s.ProjectId;
                }
                if (DateTime.Compare(sprint.DateFrom, DateTime.Now) < 0)
                {
                    ModelState.AddModelError("", "Start date of the sprint is in the past!");
                }
                else if (DateTime.Compare(sprint.DateFrom, sprint.DateTo) < 0)
                {
                    Sprint s = sprintR.GetSprint(sprint.Id);
                    if (sprint.ProjectId == null)
                        sprint.ProjectId = s.ProjectId;

                    if (IsSprintDateValid(sprint) == true)
                    {
                        
                        sprintR.UpdateSprint(sprint);
                        //db.Entry(sprint).State = EntityState.Modified;
                        //db.SaveChanges();
                        return RedirectToAction("Index", new { projectId = sprint.ProjectId });
                    }
                    else
                    {
                        ModelState.AddModelError("", "Your sprint period is overlaping with an existing sprint");
                    }

                    ViewBag.newId = s.ProjectId;
                }
                else if (DateTime.Compare(sprint.DateFrom, sprint.DateTo) == 0)
                {
                    ModelState.AddModelError("", "Start and end date of the sprint are in the same day, they have to be at least a day apart!");
                }
                else if (DateTime.Compare(sprint.DateFrom, sprint.DateTo) >= 0)
                {
                    ModelState.AddModelError("", "Start date of the sprint is after its end date!");
                }
            }
            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", sprint.ProjectId);
            ViewBag.StoryId = new SelectList(db.Stories, "Id", "Name", sprint.StoryId);
            return View(sprint);
        }

        //
        // GET: /Sprint/Delete/5
 
        public ActionResult Delete(int id)
        {
            Sprint sprint = db.Sprints.Find(id);
            ViewBag.projectId = sprint.ProjectId;
            return View(sprint);
        }

        //
        // POST: /Sprint/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Sprint sprint = db.Sprints.Find(id);
            int pid = (int)sprint.ProjectId;

            //check if the sprint is active - compare starting date with todays date
            /*
             * 
             */
            db.Sprints.Remove(sprint);
            db.SaveChanges();
            return RedirectToAction("Index", new { projectId = pid });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}